package ebusiness.ebusiness.Models.Front.ShoppingTorlley;

import ebusiness.ebusiness.Dao.GoodsDao.GoodQueryVO;
import ebusiness.ebusiness.Dao.OrderDao.OrderQueryVO;
import ebusiness.ebusiness.Dao.TorlleyDao.TorlleyQueryVO;
import ebusiness.ebusiness.Dao.WalletDao.WalletQueryVO;
import ebusiness.ebusiness.Entity.Address;
import ebusiness.ebusiness.Entity.Good;
import ebusiness.ebusiness.Entity.Order;
import ebusiness.ebusiness.Entity.Torlley;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class TorlleyImp extends TorlleyModel{
    private HashMap<Integer,Torlley> map;
    private GoodQueryVO goodQuery;
    private TorlleyQueryVO torlleyQueryVO;
    private TorlleyImp(){
        super();
        map=new HashMap<>();
        goodQuery=new GoodQueryVO();
        torlleyQueryVO=new TorlleyQueryVO();
    }
    private TorlleyImp(List<Good> gd,HashMap<Integer,Integer> num,HashMap<Integer,Boolean> select){
        super(gd,num,select);
        map=new HashMap<>();
        goodQuery=new GoodQueryVO();
        torlleyQueryVO=new TorlleyQueryVO();
    }
    public void SetUser(int id){
        this.setUser(id);
    }
    @Override
  public   int addGood(Good gd) {
        if(gd!=null){
            //if(isGoodDown)
            if(cotainGood(gd.getGood_id())){
                   int x= (int)getNums().get(gd.getGood_id())+1;
                   getNums().replace(gd.getGood_id(),x);
            }else {
                getGoods().add(gd);
                getNums().put(gd.getGood_id(),1);
                selectGood(gd.getGood_id(),true);
            }
        }

        return 0;
    }

    @Override
    public int addGood(int id) throws Exception {

        Good gd=goodQuery.getGoodByID(id);

       return addGood(gd);
    }
    public int addGood(int id,int num)throws Exception{
        Good gd=goodQuery.getGoodByID(id);
        return addGood(gd,num);
    }
    public int addGood(Good gd,int num)throws Exception{
        if(gd!=null){
            //if(isGoodDown)
            if(cotainGood(gd.getGood_id())){
                int x= getNums().get(gd.getGood_id())+num;
                getNums().replace(gd.getGood_id(),x);
            }else {
                getGoods().add(gd);
                getNums().put(gd.getGood_id(),num);
                selectGood(gd.getGood_id(),true);
            }
        }
        return 0;
    }


    @Override
  public   int removeGood(int id) {
        for(Good g:getGoods()){
            if(g.getGood_id()==id){
                getGoods().remove(g);
                getNums().remove(id);
                getSelectedList().remove(id);
                return 0;
            }
        }
        return -1;
    }

    @Override
    public int modifyGoodNum(int id, int num) {
        if(cotainGood(id)&&num>=0){
            getNums().replace(id,num);
            return 0;
        }
        return -1;
    }

    @Override
    public void show(CartView cartView) {
        int l=getGoods().size();
        List<Toy> toys=new ArrayList<>();
        for(int i=0;i<l;i++){
            Good g=getGoods().get(i);
            toys.add(new Toy(g,getNums().get(g.getGood_id()),getSelectedList().get(g.getGood_id())));
        }

        cartView.showCart(toys);
    }




    public boolean cotainGood(int id){
        for(Good g:getGoods()){
            if(g.getGood_id()==id)return true;
        }
        return false;
    }
    public void selectGood(int id,boolean select){
        if(!getSelectedList().containsKey(id)){
            getSelectedList().put(id,select);
            return;
        }
        getSelectedList().replace(id,select);
    }



    public Good getAGood(int id){
        for(Good gd:getGoods()){
            if(gd.getGood_id()==id) {
          return gd;
            }}
            return null;
            }
    public Torlley getTorlley(int id){

        return newTorlley(getAGood(id),getNums().get(id),getSelectedList().get(id));
    }
    public Torlley newTorlley(Good gd,int num,boolean selected){
        if(gd==null)return null;

        Torlley torlley =new Torlley();
        if(getUser()<=0)torlley.setCustomer_id(-1);
        else torlley.setCustomer_id(getUser());
        torlley.setGood_id(gd.getGood_id());
        torlley.setGood_num(num);
        int id;
        TorlleyQueryVO torlleyQueryVO=new TorlleyQueryVO();
        try {
            id=TorlleyQueryVO.BuildOrderID();
            torlley.setShopping_id(id);
            torlley.setNun_order_form_id(id);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(selected)
        torlley.setStatus(1);
        else torlley.setStatus(0);
        return torlley;
    }

    public double getOneSum(int id){
        double sum=0.0;

       Good g=getGoods().get(id);
       int n=getNums().get(id);
       if(g!=null)return g.getGood_price()*n;
            return 0;

    }
    public double getTotal(){
        double sum=0.0;
        for(Good g:getGoods()){
           int id =g.getGood_id();
               if(getSelectedList().get(id))
               sum=sum+g.getGood_price()*(int)getNums().get(id);


        }
        return sum;
    }
    public static TorlleyImp NewInstance(){
        return  new TorlleyImp();
    }
    public static TorlleyImp NewInstance(int uid){
        TorlleyImp tp=new TorlleyImp();
        tp.setUser(uid);
        return tp;
    }
    public static TorlleyImp NewInstance(List<Good> gd, HashMap<Integer,Integer> num, HashMap<Integer,Boolean> select){
        return new TorlleyImp(gd,num,select);
    }
    public static TorlleyImp HistoryCart(int userid) throws Exception{
        TorlleyImp tp =new TorlleyImp();
        List<Torlley> templories=tp.torlleyQueryVO.getTemplories(userid);
        if(templories.size()>0)
        for(Torlley t:templories){
            tp.addGood(t.getGood_id());
            tp.getNums().put(t.getGood_id(),t.getGood_num());
            tp.getSelectedList().put(t.getGood_id(),t.getStatus()>0?true:false);
        }
        tp.setUser(userid);
        return tp;
    }



}
